package com.ssm.zyn.action;

import com.ssm.zyn.eo.UserEOpage;
import com.ssm.zyn.model.User;
import com.ssm.zyn.service.Userservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@CrossOrigin
@Api(tags="user")
@RestController

public class UserController {
    @Autowired
    private Userservice userservice;
    @ApiOperation(value = "人员添加方法")
    @RequestMapping(value="/addUser",method = RequestMethod.GET)
    public ResponseMessage addUser(User user){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if(user!=null){
            //(2)角色名称不能为空
            if(user.getIdcard()!=null){
                //(3)添加角色数据
                IsAdd=userservice.AddUser(user);
                if(IsAdd==1){
                    return Result.success("保存成功");
                }else if(IsAdd==-1){
                    return Result.error("身份证号重复不允许添加");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "人员修改方法")
    @RequestMapping(value="/updateUser",method = RequestMethod.GET)
    public ResponseMessage updateUser(User user){
        int isUpdate=0;
        //传入的参数不能为空
        if(user!=null){
            //参数当中主键不能为0

            if (user.getUid()!=0){
                isUpdate=userservice.UpdateUser(user);
                if(isUpdate==1){

                    return Result.success("更新成功");
                }else {
                    return Result.error("身份证号名重复，更新失败");
                }
            }
        }
        return  Result.error("传入参数不正确");
    }
    @ApiOperation(value = "人员删除方法")
    @RequestMapping(value="/delUser",method = RequestMethod.GET)
    public ResponseMessage deUser(int uid){
        if (uid!=0){
            int delInt=0;
            delInt=userservice.DeleteUser(uid);
            if(delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("与中间表有关联，删除失败");
            }
        }else {
            return Result.error("删除的主键不能为0,请重新输入");
        }
    }
    @ApiOperation(value = "根据条件获取人员信息")
    @RequestMapping(value = "/getUserAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getUserAll(/*@RequestBody*/ UserEOpage userEOpage){
        Map<String, List> map=new HashMap<>();
        List<UserEOpage> listUser=userservice.getWhereAll(userEOpage);
        map.put("list",listUser);
        return Result.success("200",map);
    }
    @ApiOperation(value = "根据条件获取人员信息")
    @RequestMapping(value = "/getUserSome",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getUserSome(/*@RequestBody*/ String loginname, String password){
        Map<String, List> map=new HashMap<>();
        List<UserEOpage> listUser2=userservice.getWhereSome(loginname,password);
        if (!listUser2.isEmpty()){
        map.put("list",listUser2);
        return Result.success("200",map);
        }else {
            return Result.error("用户名或密码错误");
        }
    }

}
